﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using Dapper;
using System.Linq;
using Gx.Common;//通用类
using Gx.Model.Entity;

namespace Gx.Dal.Shared
{
    public partial class M_LogInfoDal : SqlServerHelper, IDisposable
    {
        public bool AddM_LogInfo(M_LogInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("INSERT INTO M_LogInfo");
            strSql.Append("(");
            strSql.Append("LogType,");
            strSql.Append("Url,");
            strSql.Append("Details,");
            strSql.Append("LogContenr,");
            strSql.Append("AddTime");
            strSql.Append(")");
            strSql.Append("VALUES");
            strSql.Append("(");
            strSql.Append("@LogType,");
            strSql.Append("@Url,");
            strSql.Append("@Details,");
            strSql.Append("@LogContenr,");
            strSql.Append("@AddTime");
            strSql.Append(")");
            using (SqlConnection conn = ConfigHelper.Open(ConfigHelper.ConnectionStrings)) { return conn.Execute(strSql.ToString(), model) > 0; }
        }


        public bool DelM_LogInfo(int LogId)
        {
            string strSql = "DELETE FROM M_LogInfo WHERE LogId=@LogId";
            using (SqlConnection conn = ConfigHelper.Open(ConfigHelper.ConnectionStrings))
            {
                return conn.Execute(strSql.ToString(), new { LogId }) > 0;
            }
        }

        public List<M_LogInfo> GetM_LogInfoList(string strWhere, int pageIndex, int pageSize, out int totalCount)
        {
            totalCount = GetM_LogInfoCount(strWhere);
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM (");
            strSql.Append("SELECT ROW_NUMBER() OVER(ORDER BY LogId DESC) AS ORDERBYID");
            strSql.Append(",LogId");
            strSql.Append(",LogType");
            strSql.Append(",Url");
            strSql.Append(",Details");
            strSql.Append(",LogContenr");
            strSql.Append(",AddTime");
            strSql.AppendFormat(" FROM M_LogInfo WITH(NOLOCK) WHERE 1=1 {0}) AS T WHERE T.ORDERBYID  BETWEEN {1} AND {2}", strWhere, pageIndex, pageSize);
            using (SqlConnection conn = ConfigHelper.Open(ConfigHelper.ConnectionStrings))
            {
                List<M_LogInfo> list = conn.Query<M_LogInfo>(strSql.ToString()).ToList();
                return list;
            }
        }



        public int GetM_LogInfoCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("SELECT COUNT(*) FROM M_LogInfo WITH(NOLOCK) WHERE 1=1 {0}", strWhere);
            using (SqlConnection conn = ConfigHelper.Open(ConfigHelper.ConnectionStrings))
            {
                object obj = conn.ExecuteScalar(strSql.ToString()); return obj.IntParse();
            };
        }
    }
}
